﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Viewer.PhanQuyen.UserControls;
using System.Web.Security;
using Ext.Net;
using System.Web.UI.MobileControls;

namespace Viewer
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            
        }

        protected void btnLogin_Click(object sender, DirectEventArgs e)
        {

            var oNguoiDung = new Viewer.BusinessObject.BusinessEntities.QUNguoiDung();
            bool validate = false;
            try
            {
                validate = oNguoiDung.ValidateUser(this.txtUsername.Text.Trim(), this.txtPassword.Text);
                
            }
            catch (Exception ex)
            {

                Ext.Net.X.MessageBox.Show(new MessageBoxConfig()
                {
                    Icon = Ext.Net.MessageBox.Icon.ERROR,
                    Title = "Thông Báo",
                    Message = ex.Message,
                    Buttons = MessageBox.Button.OK
                });
                return;
            }

            if (validate)
            {
                FormsAuthenticationTicket titket = new FormsAuthenticationTicket(1, this.txtUsername.Text.ToUpper(), DateTime.Now, DateTime.Now.AddHours(1), false, string.Empty);
                string str = FormsAuthentication.Encrypt(titket);
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, str);
                Response.Cookies.Add(cookie);

                InitSession(this.txtUsername.Text.ToUpper());
                Response.Redirect(FormsAuthentication.GetRedirectUrl(this.txtUsername.Text.ToUpper(), false));
            }
            else
            {
                Ext.Net.X.MessageBox.Show(new MessageBoxConfig()
                {
                    Icon = Ext.Net.MessageBox.Icon.ERROR,
                    Title = "Thông Báo",
                    Message = "Tên đăng nhập hoặc mật khẩu không đúng",
                    Buttons = MessageBox.Button.OK
                });
            }
        }

        private void InitSession(string username)
        {
            var oNguoidung = BusinessObject.BusinessEntities.QUNguoiDung.GetByUserName(username);
            Enterprise.Core.CurrentContext.Initial(oNguoidung.NguoiDungID, oNguoidung.TenDangNhap);
            var lstNhomQuyenByToChuc_ToChuc = oNguoidung.NhomQuyenByToChuc_ToChuc();
            var lstNhomQuyenByModule_Module = oNguoidung.NhomQuyenByModule_Module();
            Enterprise.Core.CurrentContext.PutIntoStorage("NhomQuyenByToChuc_ToChuc", lstNhomQuyenByToChuc_ToChuc);
            Enterprise.Core.CurrentContext.PutIntoStorage("NhomQuyenByModule_Module", lstNhomQuyenByModule_Module);
            //tam thoi de lai            
            Session["NhomQuyenByToChuc_ToChuc"] = lstNhomQuyenByToChuc_ToChuc;
            Session["NhomQuyenByModule_Module "] = lstNhomQuyenByModule_Module;
        }
    }
}
